<?php
namespace app\logistics\controller;

use think\Controller;
use think\Request;
use app\logistics\service\LogisticsRules as LogisticsRulesService;
use app\logistics\validate\LogisticsRules as LogisticsRulesValidate;

class LogisticsRules extends BaseController
{
    /**
     * 物流规则列表
     * @param Request $request 搜索条件
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-7-2
     */
    public function getList(Request $request)
    {
        $param['rule_name'] =  trim((string)$request->param('rule_name',null));
        $param['logistics_name'] =  trim((string)$request->param('logistics_name',null));
        $param['terms'] =  trim((string)$request->param('terms',null));
        $param['from_nation'] =  trim((string)$request->param('from_nation',null));
        $param['to_nation'] =  trim((string)$request->param('to_nation',null));
        $param['is_active'] = trim((string)$request->param('is_active',null));
        $param['page'] =  $request->param('page',1);
        $param['pagesize']  =  $request->param('pagesize',50);
        $validate = new LogisticsRulesValidate();
        if (!$validate->scene('list')->check($param)) {
            return json($validate->getError());
        }
        $LogisticsRulesService = new LogisticsRulesService();
        $arrData = $LogisticsRulesService->getList($param);
        return json($arrData);
    }

    /**
     * 获取激活的物流规则
     * @param Request $request 搜索条件
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-7-5
     */
    public function getActiveList(Request $request)
    {
        $param['logistics_name'] =  trim((string)$request->param('logistics_name',null));
        $param['terms'] =  trim((string)$request->param('terms',null));
        $validate = new LogisticsRulesValidate();
        if (!$validate->scene('active')->check($param)) {
            return json($validate->getError());
        }
        $LogisticsRulesService = new LogisticsRulesService();
        $arrData = $LogisticsRulesService->getActiveList($param);
        return json($arrData);
    }

    /**
     * 物流规则详情
     * @param Request $request 物流规则ID
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-7-2
     */
    public function getInfo(Request $request)
    {
        $param['id'] = $request->param('id',null);
        $validate = new LogisticsRulesValidate();
        if (!$validate->scene('info')->check($param)) {
            return json($validate->getError());
        }
        $LogisticsRulesService = new LogisticsRulesService();
        $arrData = $LogisticsRulesService->getInfo($param['id']);
        return json($arrData);
    }

    /**
     * 添加物流规则
     * @param Request $request 物流规则数据
     * @return \think\response\Json 返回成功或失败
     * @author huangfeng
     * @date 2024-7-2
     */
    public function add(Request $request)
    {
        $param['rule_name'] = trim((string)$request->param('rule_name',null));
        $param['logistics_name'] = trim((string)$request->param('logistics_name',null));
        $param['terms'] = trim((string)$request->param('terms',null));
        $param['freight_tag'] = trim((string)$request->param('freight_tag',null));
        $param['from_nation'] = trim((string)$request->param('from_nation',null));
        $param['to_nation'] = trim((string)$request->param('to_nation',null));
        $param['model'] = trim((string)$request->param('model',null));
        $param['weight_lower'] = $request->param('weight_lower',0.00);
        $param['weight_upper'] = $request->param('weight_upper',0.00);
        $param['transfer_fee'] = $request->param('transfer_fee',0.00);
        $param['single_fee'] = $request->param('single_fee',0.00);
        $param['magnetic_inspection_fee'] = $request->param('magnetic_inspection_fee',0.00);
        $param['customs_declaration_fee'] = $request->param('customs_declaration_fee',0.00);
        $param['insurance_rate'] = $request->param('insurance_rate',0.00);
        $param['insurance_fee'] = $request->param('insurance_fee',0.00);
        $param['freight'] = $request->param('freight',0.00);
        $param['level'] = $request->param('level',99);
        $param['is_active'] = trim((string)$request->param('is_active','1'));
        $param['notes'] = trim((string)$request->param('notes',null));
        $param['creator'] = $request->param('userid',0);
        $validate = new LogisticsRulesValidate();
        if (!$validate->scene('add')->check($param)) {
            return json($validate->getError());
        }
        $LogisticsRulesService = new LogisticsRulesService();
        $arrData = $LogisticsRulesService->add($param);
        return json($arrData);
    }

    /**
     * 更新物流规则
     * @param Request $request 物流规则数据
     * @return \think\response\Json 返回成功或失败
     * @author huangfeng
     * @date 2024-7-2
     */
    public function update(Request $request)
    {
        $param['id'] = $request->param('id',0);
        $param['rule_name'] = trim($request->param('rule_name',null));
        $param['logistics_name'] = trim($request->param('logistics_name',null));
        $param['terms'] = trim($request->param('terms',null));
        $param['freight_tag'] = trim($request->param('freight_tag',null));
        $param['from_nation'] = trim($request->param('from_nation',null));
        $param['to_nation'] = trim($request->param('to_nation',null));
        $param['model'] = trim($request->param('model',null));
        $param['weight_lower'] = $request->param('weight_lower',0.00);
        $param['weight_upper'] = $request->param('weight_upper',0.00);
        $param['transfer_fee'] = $request->param('transfer_fee',0.00);
        $param['single_fee'] = $request->param('single_fee',0.00);
        $param['magnetic_inspection_fee'] = $request->param('magnetic_inspection_fee',0.00);
        $param['customs_declaration_fee'] = $request->param('customs_declaration_fee',0.00);
        $param['insurance_rate'] = $request->param('insurance_rate',0.00);
        $param['insurance_fee'] = $request->param('insurance_fee',0.00);
        $param['freight'] = $request->param('freight',0.00);
        $param['level'] = $request->param('level',99);
        $param['is_active'] = trim($request->param('is_active','1'));
        $param['notes'] = trim($request->param('notes',null));
        $validate = new LogisticsRulesValidate();
        if (!$validate->scene('update')->check($param)) {
            return json($validate->getError());
        }
        $LogisticsRulesService = new LogisticsRulesService();
        $arrData = $LogisticsRulesService->update($param);
        return json($arrData);
    }

    /**
     * 删除物流规则
     * @param Request $request 物流规则ID
     * @return \think\response\Json 返回成功或失败
     * @author huangfeng
     * @date 2024-7-2
     */
    public function delete(Request $request)
    {
        $param['id'] = $request->param('id',null);
        $validate = new LogisticsRulesValidate();
        if (!$validate->scene('delete')->check($param)) {
            return json($validate->getError());
        }
        $LogisticsRulesService = new LogisticsRulesService();
        $arrData = $LogisticsRulesService->delete($param['id']);
        return json($arrData);
    }
}
